﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BlackLight
{
    public partial class App : Application
    {

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {

            //explanation : ChartName£ColumnType£ColumnName£DataName£

            //string PoshCharts = "dEUZµlabelhorizonµverticalmaraceµ£Barµ£handlecountµ£AcroRd32µAEstSrvµApMsgFwdµApntExµApointµashDispµashMaiSvµashServµashWebSvµaswUpdSvµaudiodgµBcmSqlStartupSvcµBTStackServerµBTTrayµchromeµchromeµchromeµchromeµchromeµchromeµchromeµchromeµchromeµchromeµchromeµconimeµcsrssµcsrssµdwmµE_FATIBVEµehmsasµehschedµehtrayµEvtEngµexplorerµfirefoxµgoogletalkpluginµGoogleToolbarNotifierµGoogleUpdateµhidfindµIAAnotifµIAANTmonµIdleµjuschedµlsassµlsmµMSASCuiµmsnmsgrµnakidoµnotepadµOEM02MonµPCMServiceµPOWERPNTµpowershellµPresentationFontCacheµpsqltrayµRegSrvcµrundll32µrundll32µrundll32µSearchIndexerµSEPCSuiteµservicesµSkylightµSLsvcµsmssµspoolsvµsprtcmdµsprtsvcµsqlservrµsqlservrµsqlwriterµstacsvµStartFXµStarWindServiceAEµSteamµSteamServiceµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµsvchostµSystemµtaskengµtaskengµTSVNCacheµupeksvrµusnsvcµVirtual PCµVWDExpressµVWDExpressµWebDev.WebServerµwininitµwinlogonµwlanextµWmiPrvSEµWmiPrvSEµXBoxStatµ£171¤µ27¤µ33¤µ64¤µ111¤µ142¤µ128¤µ378¤µ189¤µ30¤µ143¤µ52¤µ472¤µ148¤µ112¤µ113¤µ103¤µ897¤µ121¤µ110¤µ112¤µ113¤µ260¤µ114¤µ833¤µ32¤µ788¤µ1019¤µ318¤µ59¤µ53¤µ85¤µ109¤µ240¤µ1279¤µ378¤µ151¤µ254¤µ140¤µ52¤µ58¤µ158¤µ0¤µ47¤µ983¤µ216¤µ537¤µ911¤µ219¤µ55¤µ79¤µ155¤µ840¤µ360¤µ140¤µ339¤µ68¤µ99¤µ79¤µ46¤µ1385¤µ697¤µ291¤µ650¤µ108¤µ28¤µ315¤µ321¤µ296¤µ323¤µ296¤µ80¤µ142¤µ80¤µ111¤µ476¤µ233¤µ338¤µ460¤µ388¤µ531¤µ701¤µ1524¤µ518¤µ659¤µ360¤µ109¤µ123¤µ245¤µ79¤µ4776¤µ490¤µ132¤µ136¤µ264¤µ72¤µ256¤µ958¤µ1503¤µ698¤µ101¤µ130¤µ391¤µ127¤µ127¤µ98¤µ£§";
            //string PoshCharts = "GraphMaaanµlabelhorizonµverticalmaraceµ£StackedColumnµStackedColumnµ£Sizeµfreespaceµ£C:µD:µ£236512600064¤18388365312¤µ10737414144¤5300219904¤µ£§";
            //PoshCharts = "GraphMaaanµlabelhorizonµverticalmaraceµ£ColumnµColumnµ£Sizeµfreespaceµ£C:µD:µ£236512600064¤18437685248¤µ10737414144¤5300219904¤µ£§";

            string PoshCharts = "";
            if (e != null)
            {
                PoshCharts = e.InitParams["ChartData"];
            }

            this.RootVisual = new Page(PoshCharts);
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }
        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }
    }
}
